package com.cenxi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cx.websocket.dto.SendMessages;
import com.cx.websocket.websocket.MyWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author cenxi
 * @Date 2023/10/9 11:00
 * @Version 1.0
 */
@RequestMapping("/socket")
@RestController
@Slf4j
public class WebSocketController {
    @Resource
    private MyWebSocket webSocket;

    @PostMapping("/sendAllMessage")
    public Object sendAllMessage(@RequestBody SendMessages sendMessages) {
        webSocket.sendAllMessage(sendMessages.getMessage());
        log.info("全部消息发送成功--> {}", sendMessages.getMessage());
        return "全部消息发送成功--> "+ sendMessages.getMessage();
    }

    @PostMapping("/sendOneMessage")
    public Object sendOneMessage(@RequestBody SendMessages sendMessages) {
        webSocket.sendOneMessage(sendMessages.getUserIds().get(0), sendMessages.getMessage());
        log.info("单人消息发送成功--> {}, userId: {}", sendMessages.getMessage(), sendMessages.getUserIds().get(0));
        return "单人消息发送成功--> "+ sendMessages.getMessage() + "userId: " + sendMessages.getUserIds().get(0);
    }

    @PostMapping("/sendMoreMessage")
    public Object sendMoreMessage(@RequestBody SendMessages sendMessages) {
        webSocket.sendMoreMessage(sendMessages.getUserIds(), sendMessages.getMessage());
        log.info("多人消息发送成功--> {}, userId: {}", sendMessages.getMessage(), sendMessages.getUserIds().get(0));
        return "多人消息发送成功--> "+ sendMessages.getMessage() + "userIds: " + sendMessages.getUserIds();
    }

    @GetMapping("/get/{userId}")
    public Object get(@PathVariable("userId") String userId) {
        return webSocket.onlineUser(userId);
    }
}
